package com.fary.validator;

import com.fary.vo.User;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class UserValidator implements Validator {

	/**
	 * This Validator validates only Person instances
	 */
	public boolean supports(Class clazz) {
		return User.class.equals(clazz);
	}

	public void validate(Object obj, Errors e) {
		ValidationUtils.rejectIfEmpty(e, "name", "当前的用户名为空，请输入用户名！");
		// ValidationUtils.rejectIfEmptyOrWhitespace();//用于校验当前的对象的属性视为为null或者" "
		User user = (User) obj;
		if (user.getAge() <= 0) {
			e.rejectValue("age", "当前的用户年龄太小，不合法！");
		} else if (user.getAge() > 110) {
			e.rejectValue("age", "当前用户的年龄太大，不合法！");
		}
	}
}
